package com.kfm.am;

import java.util.Vector;

public class Test03 {

    public static void main(String[] args) {
        // 创建一个Vector集合，用于存储一组整数。编写一个函数，接受一个整数并返回在Vector中出现的次数。
        Vector vector = new Vector();
        vector.add(1);
        vector.add(1);
        vector.add(1);
        vector.add(2);
        vector.add(3);
        vector.add(4);
        System.out.println(countNumber1(vector, 1));
    }

    public static int countNumber(Vector vector, int number) {
        int count = 0;
        for (int i = 0; i < vector.size(); i ++) {
            if ((int)vector.elementAt(i) == number) {
                count ++;
            }
        }
        return count;
    }

    public static int countNumber1(Vector vector, int number) {
        int index = vector.indexOf(number);
        if (index == -1) {
            return 0;
        }
        int count = 1;
        while (true) {
            index = vector.indexOf(number, index + 1);
            if (index == -1) {
                return count;
            }
            count++;
        }
    }
}
